package com.mes.cloud.web;

import com.mes.cloud.api.dto.request.UpdateEnableRequest;
import com.mes.cloud.api.dto.request.organization.SysOrganizationPageRequest;
import com.mes.cloud.api.dto.request.organization.SysOrganizationPositionAuthRequest;
import com.mes.cloud.api.dto.request.organization.SysOrganizationRequest;
import com.mes.cloud.api.dto.request.organization.SysOrganizationUpdateRequest;
import com.mes.cloud.api.dto.response.organization.SysOrganizationDetailsDto;
import com.mes.cloud.api.dto.response.organization.SysOrganizationDto;
import com.mes.cloud.api.dto.response.organization.SysOrganizationTreeAuthDto;
import com.mes.cloud.api.dto.response.organization.SysOrganizationTreeDto;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.service.SysOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xy
 * @version 2019-07-30 09:35:59
 * @email yuan.xue3@pactera.com
 */
@Api(tags = "组织", value = "组织接口")
@RestController
@RequestMapping(value = "/sysOrganization")
public class SysOrganizationController  {

    private SysOrganizationService sysOrganizationService;

    @Autowired
    public SysOrganizationController(SysOrganizationService sysOrganizationService) {
        this.sysOrganizationService = sysOrganizationService;
    }

    /**
     * <分页查询数据>
     *
     * @param pageRequest
     * @return JsonResult
     * @author xy
     * @date 2019/8/2 10:53
     */
    @ApiOperation(value = "组织表分页查询", notes = "组织表分页查询")
    @PostMapping(value = "/listPage")
    public JsonResult<CommPager<SysOrganizationDto>> listPage(@RequestBody SysOrganizationPageRequest pageRequest) {
        CommPager<SysOrganizationDto> commPager = sysOrganizationService.selectPage(pageRequest);
        JsonResult<CommPager<SysOrganizationDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpStatusEnum.OK.getCode()).setData(commPager);
    }

    /**
     * <分页查询数据>
     *
     * @param sysOrganizationPositionAuthRequest
     * @return JsonResult
     * @author xy
     * @date 2019/8/2 10:53
     */
    @ApiOperation(value = "组织表查询所有", notes = "组织表查询所有")
    @PostMapping(value = "/list")
    public JsonResult<List<SysOrganizationTreeDto>> list(@Valid @RequestBody SysOrganizationPositionAuthRequest sysOrganizationPositionAuthRequest) {
        List<SysOrganizationTreeDto> list = sysOrganizationService.list(sysOrganizationPositionAuthRequest);
        JsonResult<List<SysOrganizationTreeDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpStatusEnum.OK.getCode()).setData(list);
    }

    /**
     * <新增数据>
     *
     * @param sysOrganizationRequest
     * @return JsonResult
     * @author xy
     * @date 2019/8/2 10:53
     */
    @ApiOperation(value = "组织表新增", notes = "组织表新增")
    @PostMapping(value = "/add")
    public JsonResult<SysOrganizationDto> add(@RequestBody SysOrganizationRequest sysOrganizationRequest) {
        return sysOrganizationService.insertSelective(sysOrganizationRequest);
    }

    /**
     * <更新数据>
     *
     * @param sysOrganizationRequest
     * @return JsonResult
     * @author xy
     * @date 2019/8/2 10:51
     */
    @ApiOperation(value = "组织表修改", notes = "组织表修改")
    @PostMapping(value = "/update")
    public JsonResult update(@RequestBody SysOrganizationRequest sysOrganizationRequest) {
        return sysOrganizationService.updateSelectiveById(sysOrganizationRequest);
    }

    /**
     * <查询组织详细信息>
     *
     * @param id
     * @return JsonResult
     * @author xy
     * @date 2019/8/2 16:03
     */
    @ApiOperation(value = "组织表查看详情", notes = "组织表查看详情")
    @ApiImplicitParam(paramType = "String", name = "id", value = "组织表主键", required = true)
    @GetMapping(value = "get/{id}")
    public JsonResult<SysOrganizationDetailsDto> get(@PathVariable String id) {
        SysOrganizationDetailsDto sysOrganizationDetailDto = sysOrganizationService.get(id);
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setCode(HttpStatusEnum.OK.getCode()).setData(sysOrganizationDetailDto);
    }

    /**
     * <禁用启用>
     *
     * @param updateEnableRequest
     * @return JsonResult
     * @author xy
     * @date 2019/8/2 10:52
     */
    @ApiOperation(value = "组织表禁用启用", notes = "组织表禁用启用")
    @PostMapping(value = "/updateEnableFlag")
    public JsonResult updateEnableFlag(@Valid @RequestBody UpdateEnableRequest updateEnableRequest) {
        return sysOrganizationService.updateEnableFlag(updateEnableRequest);
    }

    /**
     * <查询员工授权组织树>
     *
     * @return JsonResult
     * @author xy
     * @date 2019/8/2 10:53
     */
    @ApiOperation(value = "查询组织树", notes = "查询组织树")
    @PostMapping(value = "/treeList")
    public JsonResult<List<SysOrganizationTreeAuthDto>> treeList() {
        List<SysOrganizationTreeAuthDto> list = sysOrganizationService.treeList();
        JsonResult<List<SysOrganizationTreeAuthDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpStatusEnum.OK.getCode()).setData(list);
    }

    @ApiOperation(value = "排除查询授权组织", notes = "排除查询授权组织")
    @PostMapping(value = "/excludeQueryAuthOrganization")
    public JsonResult<List<SysOrganizationDto>> excludeQueryAuthOrganization(@Valid @RequestBody SysOrganizationUpdateRequest request) {
        return sysOrganizationService.excludeQueryAuthOrganization(request);
    }

    @ApiOperation(value = "查询组织、子集", notes = "查询组织、子集")
    @PostMapping(value = "/listChildren")
    public JsonResult<List<String>> listChildren(@RequestBody List<String> orgAuthIds){
        return sysOrganizationService.listChildren(orgAuthIds);
    }

    @ApiOperation(value = "查询父级", notes = "查询父级")
    @PostMapping(value = "/getOrgParent")
    public JsonResult<SysOrganizationDto> getOrgParent(@RequestBody SysOrganizationRequest sysOrganizationRequest){
        return sysOrganizationService.getOrgParent(sysOrganizationRequest);
    }

}
